package com.baizhi.exception;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * restful前后端分离的异常处理
 */

@RestControllerAdvice
public class GlobalExceptionResolver {

    /**
     * 处理非法参数的异常
     */
    @ExceptionHandler(IllegalNumberException.class)
    public ResponseEntity<String> illegalNumberExceptionHandler(IllegalNumberException e) {
        return new ResponseEntity<String>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理所有的异常，兜底，放在最下面
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e) {
        return new ResponseEntity<>(e.getMessage(),HttpStatus.NETWORK_AUTHENTICATION_REQUIRED);
    }
}
